import { defineConfig, ConfigEnv, UserConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: "./",
    plugins: [
      vue(), // 自动导入参考： https://github.com/sxzz/element-plus-best-practices/blob/main/vite.config.ts
      AutoImport({
        // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
        imports: ["vue", "pinia", "vue-router"],
        eslintrc: {
          enabled: false,
          filepath: "./.eslintrc-auto-import.json",
          globalsPropValue: true,
        },
        vueTemplate: true,
        // 配置文件生成位置(false:关闭自动生成)
        dts: false,
        // dts: "src/typings/auto-imports.d.ts",
      }),
      Components({
        // 指定自定义组件位置(默认:src/components)
        dirs: ["src/components", "src/**/components"],
        // 配置文件位置 (false:关闭自动生成)
        dts: false,
        // dts: "src/typings/components.d.ts",
      }),
    ],
    resolve: {
      alias: {
        "@": "/src",
        "@commponents": "/src/components",
      },
    },

    build: {
      // 引用“terser”插件,打包时取出console输出语句
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: false,
          drop_debugger: true,
        },
      },
    },
    server: {
      host: true,
      proxy: {
        /**
         * 代理前缀为 /dev-api 的请求
         */
        [env.VITE_APP_BASE_API1]: {
          changeOrigin: true,
          // 接口地址
          target: env.VITE_APP_API_URL1,
          rewrite: (path: any) => {
            return path.replace(new RegExp("^" + env.VITE_APP_BASE_API1), "");
          },
        },
      },
    },
  };
});
